﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Valentia.Kinection.Service.Manager
{
    /// <summary>
    /// This form is used to show service clients and their connectivity information
    /// </summary>
    public partial class ServiceClientsConnectivityInfo : Form
    {
        #region Constructor

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="serviceClientList">DataTable contains the information regarding service clients</param>
        public ServiceClientsConnectivityInfo(DataTable serviceClientList)
        {
            InitializeComponent();

            // Registering listview event
            this.lvwServiceClientConnectivityStatus.DrawItem 
                += new DrawListViewItemEventHandler(OnDrawItem); 

            // Postioning the form in the center of the screen
            this.StartPosition = FormStartPosition.CenterScreen;

            BindListView(serviceClientList);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// This method is used to bind service clients related information table to list view
        /// </summary>
        /// <param name="serviceClientList">DataTable contains the information regarding service clients</param>
        private void BindListView(DataTable serviceClientList)
        {
            try
            {
                foreach (DataRow serviceClientInfo in serviceClientList.Rows)
                {
                    lvwServiceClientConnectivityStatus.Items.Add(
                        new ListViewItem(new string[] { serviceClientInfo["ClientName"].ToString()
                        , serviceClientInfo["ClientType"].ToString(), serviceClientInfo["Connected"].ToString()}));
                }
            }
            catch { }
        }

        #endregion      
  
        #region Form Control Events

        /// <summary>
        /// Occurs when a System.Windows.Forms.ListView is drawn 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDrawItem(object sender, DrawListViewItemEventArgs e)
        { 
            try
            {
                foreach (ListViewItem.ListViewSubItem subItem in e.Item.SubItems)
                {
                    e.Item.UseItemStyleForSubItems = false;
                   
                    subItem.BackColor = e.ItemIndex % 2 == 0 
                        ? Color.FromArgb(209, 234, 251) : Color.FromArgb(189, 218, 238);

                    subItem.ForeColor = e.ItemIndex % 2 == 0 
                        ? Color.FromArgb(0, 78, 152) : Color.FromArgb(0, 78, 152);
                    
                    subItem.Font = e.Item.Font;
                }
            }
            catch { }

            e.DrawDefault = true;        
        }

        #endregion

    }
}
